var userId = null;



function getImageFullURLPath(avatar_url) {
    return "http://localhost:8080" + avatar_url;
}

function checkLogin(){
    // console.log("checking login")
    $.post({
        url: 'http://localhost:8080/login/checkLogin',
        async:false,
        method: 'POST',
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            let user = msg.data.user;
            window.userId = user.id;
        }, error: function (error) {
            console.log(error)
        }
    })
}

function loadUser() {
    var id = getQueryVariable("id");
    if(id === false){
        id = null;
    }
    let username = getQueryVariable("username");
    if(typeof username === "boolean"){
        username = null;
    }
    $.post({
        url: 'http://localhost:8080/user/info',
        async:false,
        method: 'POST',
        data:{
            "id":id,
            "username" : username
        },
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            console.log(msg)
            let user = msg.data.user;
            //    user是
            console.log(user)
            if( user === undefined || user === null){
                $("#main-div").empty();
                layer.msg("这个用户不存在~");
                window.setTimeout(function (){
                    window.history.back();
                },3000)
            }
            if(user !== undefined && user !== null){ console.log("user saved"); window.user = user;}
            console.log(window.user);
        }, error: function (error) {
            layer.msg("公告获取失败~");
            console.log(error)
        }
    })
}

function loadData(){
    checkLogin();
    loadUser();
    if(user === undefined){
        return;
    }
    if(user.base.id === userId){
        alert(user.base.id + " " + userId)
        $("#change-info").css("display","display");
    }
    let base = window.user.base;
    let more = window.user.more;
    $("#avatar-inner img").attr("src",getImageFullURLPath(more.avatarUrl));
    $("#nickname").text(more.nickname);
    $("#username").text("@"+base.username);
    $("#sign").text(more.signature);
    $("#post-cnt").text(base.post_count);
    $("#reply-cnt").text(base.reply_count);
    $("#sex").text(more.gender);
    $("#province").text(more.provinceName);
    $("#city").text(more.cityName);
    $("#last-login-time").text(base.last_login_time);
    $("#regis-time").text(base.create_time);
}

function changeInfo(){
    window.location.href='./edit.html'
}